package cn.knightzz.service.impl;

import cn.knightzz.entity.SysRepository;
import cn.knightzz.mapper.SysRepositoryMapper;
import cn.knightzz.service.SysRepositoryService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author knightzz
 * @since 2021-05-09
 */
@Slf4j
@Service
public class SysRepositoryServiceImpl extends ServiceImpl<SysRepositoryMapper, SysRepository> implements SysRepositoryService {

    @Resource
    SysRepositoryMapper repositoryMapper;

    /**
     * 添加知识库
     *
     * @param repository
     * @return
     */
    @Override
    public Boolean addRepository(SysRepository repository) {
        int row = 0;
        try {
            row = repositoryMapper.insert(repository);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return row != 0;
    }

    /**
     * 编辑知识库
     *
     * @param repository
     * @return
     */
    @Override
    public Boolean editRepository(SysRepository repository) {
        int row = 0;
        try {
            row = repositoryMapper.updateById(repository);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return row != 0;
    }

    /**
     * 删除知识库内容
     *
     * @param repoId
     * @return
     */
    @Override
    public Boolean deleteRepository(Long repoId) {
        int row = 0;
        try {
            row = repositoryMapper.deleteById(repoId);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return row != 0;
    }

    /**
     * 获取所有的列表
     *
     * @return
     */
    @Override
    public List<SysRepository> getRepositoryList() {
        try {
            return repositoryMapper.selectList(null);
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }

    /**
     * 分页查询数据
     *
     * @param pageNum  页码
     * @param pageSize 每页数据条数
     * @return
     */
    @Override
    public IPage<SysRepository> selectRepoPage(Long pageNum, Long pageSize, String query) {

        Page<SysRepository> page = new Page<>();
        page.setSize(pageSize);
        page.setCurrent(pageNum);

        // 构造查询参数
        QueryWrapper<SysRepository> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("title", query);
        queryWrapper.orderByDesc("update_time");

        try {
            return repositoryMapper.selectPage(page, queryWrapper);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

}
